﻿using RealtorsPortal.Business_Logic_Layer;
using RealtorsPortal.Data_Access_Layer;
using RealtorsPortal.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RealtorsPortal.AdminCP
{
    public partial class RenewAdvertisement : System.Web.UI.Page
    {
        DataAccessHelper dah = new DataAccessHelper();
        ParkagesDAL packDAL = new ParkagesDAL();
        AdvertisementsDAO advObj = new AdvertisementsDAO();
        AdvertisementsDAL advDAL = new AdvertisementsDAL();
        AccountDAL accDAL = new AccountDAL();
        Package packObj = new Package();
        int month;
        int year;
        int day;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["ID"] != null)
            {
                advObj.AdvId = Convert.ToInt32(Request.QueryString["ID"].ToString());
            }
            if(!IsPostBack){
                GetPackage();
                HideDiv();
            }
            
        }

        private void GetPackage() {
            DataTable dt = new DataTable();
            dt = packDAL.getPackagesData();
            grvPayPal.DataSource = dt;
            grvPayPal.DataBind();
        }

        protected void grvPayPal_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            ShowDiv();
            if (e.CommandName == "PayPalComponent")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                packObj.PackageId = Convert.ToInt16(grvPayPal.DataKeys[index].Value.ToString());
                //Response.Redirect("~/AdminCP/EditAdvertisement.aspx?ID="+AdvId);
                Utilities.temp = GetDurationPackage(packObj);
            }
            ShowInformationCustomer();
        }

        private int GetDurationPackage(Package pObj) {
            DataTable dt = new DataTable();
            dt = packDAL.GetPackageById(pObj);

            return Convert.ToInt16(dt.Rows[0]["Duration"].ToString()); 
        }

        protected int GetCurrentSession()
        {

            return Convert.ToInt16(Session["adminId"]);
        }

        private int UpdateDuration() {
            DataTable dt = new DataTable();
            dt = GetExpAdvertisement();
            
            advObj.ExpDate = dt.Rows[0]["ExpDate"].ToString();
            DateTime db = DateTime.Parse(advObj.ExpDate);

            if (db.Month == 1 || db.Month == 3 || db.Month == 5 || db.Month == 7 || db.Month == 8 || db.Month == 10 || db.Month == 12)
            {
                if (db.Day + Utilities.temp > 31)
                {
                    day = db.Day + Utilities.temp - 31;
                    month = (db.Month + 1);
                    if (month > 12)
                    {
                        month = 1;
                        year = db.Year + 1;
                    }
                    else
                    {
                        year = db.Year;
                    }
                }
                else
                {
                    day = db.Day + Utilities.temp;
                    month = db.Month;
                    year = db.Year;
                }
            }
            else if (db.Month == 4 || db.Month == 6 || db.Month == 9 || db.Month == 11)
            {
                if (db.Day + Utilities.temp > 30)
                {
                    day = db.Day + Utilities.temp - 30;
                    month = (db.Month + 1);
                    if (month > 12)
                    {
                        month = 1;
                        year = db.Year + 1;
                    }
                    else
                    {
                        year = db.Year;
                    }
                }
                else
                {
                    day = db.Day + Utilities.temp;
                    month = db.Month;
                    year = db.Year;
                }
            }
            else if (db.Month == 2)
            {
                if (db.Year % 400 == 0)
                {
                    if (db.Day + Utilities.temp > 29)
                    {
                        day = db.Day + Utilities.temp - 29;
                        month = (db.Month + 1);
                        if (month > 12)
                        {
                            month = 1;
                            year = db.Year + 1;
                        }
                        else
                        {
                            year = db.Year;
                        }
                    }
                    else
                    {
                        day = db.Day + Utilities.temp;
                        month = db.Month;
                        year = db.Year;
                    }
                }
                else
                {
                    if (db.Day + Utilities.temp > 28)
                    {
                        day = db.Day + Utilities.temp - 28;
                        month = (db.Month + 1);
                        if (month > 12)
                        {
                            month = 1;
                            year = db.Year + 1;
                        }
                        else
                        {
                            year = db.Year;
                        }
                    }
                    else
                    {
                        day = db.Day + Utilities.temp;
                        month = db.Month;
                        year = db.Year;
                    }
                }
            }

            advObj.ExpDate = (month + "-" + day + "-" + year).ToString();
            
            int result = advDAL.UpdateDuration(advObj);
            return result;
        }

        private DataTable GetExpAdvertisement() {
            DataTable dt = new DataTable();
            dt = advDAL.GetImageUrlById(advObj);
            return dt;
        }

        private void ShowInformationCustomer() {
            DataTable dt = new DataTable();
            Account accObj = new Account();
            accObj.AccountId = GetCurrentSession();
           dt =  accDAL.GetUserTable(accObj);
           txtFirtName.Text = dt.Rows[0]["FirstName"].ToString();
           txtLastName.Text = dt.Rows[0]["LastName"].ToString();
           txtAddress.Text = dt.Rows[0]["Address"].ToString();
           txtEmail.Text = dt.Rows[0]["Email"].ToString();
           txtPhone.Text = dt.Rows[0]["Phone"].ToString();
           
        }

        private void HideDiv() {
            divAddress.Visible = false;
            divCardNumber.Visible = false;
            divEmail.Visible = false;
            divFirstName.Visible = false;
            divLastName.Visible = false;
            divPhone.Visible = false;
            divImage.Visible = false;
            divButton.Visible = false;
        }

        private void ShowDiv() {
            divAddress.Visible = true;
            divCardNumber.Visible = true;
            divEmail.Visible = true;
            divFirstName.Visible = true;
            divLastName.Visible = true;
            divPhone.Visible = true;
            divImage.Visible = true;
            divButton.Visible = true;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int rs = UpdateDuration();
            if (rs > 0)
            {
                InsertIntoSubscription();
                string message = "Congratulation! You are renewed successful!";
                string url = "RenewAdvertisement.aspx";
                Utilities.ShowAndRedirect(message,url);
            }
            else
            {
                string message = "Sorry! Your renew is not successful!";
                string url = "RenewAdvertisement.aspx";
                Utilities.ShowAndRedirect(message, url);
            }
            
        }

        private DateTime GetCurrentDate()
        {
            return DateTime.Now;
        }

        private  void InsertIntoSubscription(){
            DataTable dt = new DataTable();
            Subcription subObj = new Subcription();
            subObj.SubDate = GetCurrentDate().ToString("MM-dd-yyyy");
            subObj.UserId = GetCurrentSession();
            dt = advDAL.GetPackageIdByAdvId(advObj);
            subObj.PackageId = int.Parse(dt.Rows[0]["PackageId"].ToString());
            subObj.IsPaid = true;
            SubcriptionDAL subDAL = new SubcriptionDAL();
            dt = subDAL.InsertToSubcription(subObj);

        }
    }
}